package com.zhuss.product.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.preline.StartPrelineBo;
import com.zhuss.bean.vo.product.preline.DetailPrelineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.entity.Preline;
import com.zhuss.util.JsonUtil;

/**
 * 创建招募 processor
 * @Author: zhushanshuo
 * @Date: Wed Jan 06 16:56:43 CST 2021
 */
@Component("prelineStart")
public class PrelineStart extends AbstractProcessor<Object, StartPrelineBo> implements Processor<Object, StartPrelineBo> {

    @Autowired
    private PrelineDao prelineDao;
    
    @Autowired
	@Qualifier("prelineRead")
	private PrelineRead prelineRead;
    
    @Autowired
    private PrelineCheck prelineCheck;
    
    /**
     * 校验
     */
    @Override
    public void check(StartPrelineBo bo) throws DzcxException {
    	// 当前招募不满足启用条件
//		if (!prelineCheck.checkStart(bo.getPrelineId())) {
//			throw new DzcxException(ExceptionCode.当前招募不满足启用条件);
//		}
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public Object process(StartPrelineBo param) throws DzcxException {
    	
    	Long prelineId = param.getPrelineId();
    	
    	Preline record = new Preline();
    	record.setPrelineId(param.getPrelineId());
    	record.setStatus(StartStopEnum.启用.key());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	prelineDao.updateByPrimaryKeySelective(record);
    	
    	String name = ((DetailPrelineVo) prelineRead.detail(prelineId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(StartPrelineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.招募新线启用.key());
    	record.setTargetId(param.getPrelineId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
